/**
 * @name Core Class
 * @author Joe Cianflone
 * @version 5.1
 * @copyright CC, Creative Commons Attribution-ShareAlike 3.0 Unported -- http://creativecommons.org/licenses/by-sa/3.0/
 */
/**
 *  
 * @class
 */
var LoadScripts = {
	/**
	 * checks all the different and various ways you may load up scripts and 
	 * tries to find the page name, if it can be found, it checks the pageName
	 * with scripts to see if it needs to load any files up for it
	 * @param {Object} obj
	 */
	load: function(obj) {
		if (typeof(obj) == 'string') {
			this.include(obj+'.js');	
		}
		else {
			var qsCheck = this.loadByQueryString();
			if (!qsCheck) {
				var pageLocation = this.getPageLocation();
				this._loadUp(obj, pageLocation);					
			}
		}	
	},//load()...
	/**
	 * checks the file name in the address bar and loads any files that need 
	 * to be loaded based on that address 
	 * @param {Object} obj
	 */
	loadByFileName: function(obj) {
		var file = this.getFileName();
		this._loadUp(obj, file);
		
	},//loadByFileName()...
	/**
	 * checks the <meta> tag name and loads any files that need to be loaded
	 * based on that tags name
	 * @param {Object} obj
	 */
	loadByMetaTag: function(obj) {
		var meta = this.getMetaName();
		this._loadUp(obj, meta);
	},//loadByMetaTag()...
	/**
	 * checks the <head> tag name and loads any files that need to be loaded
	 * based on that tags named
	 * @param {Object} obj
	 */
	loadByHeadTag: function(obj) {
		var head = this.getHeadElement;
		this._loadUp(obj, head);
	},//loadByHeadTag()...
	/**
	 * checks for the 'load=' query string and loads all the files you attach.
	 */
	loadByQueryString: function() {
		var s = this.initScript.src.indexOf('load=');
		if (s != -1) {
			s += 5; //this is the value of the string 'load=' so you bypass that when you get the substring
			var query = this.initScript.src.substring(s).split('&');

			if (query.length > 0) {
				for (var j = 0, len2 = query.length; j < len2; ++j) {
					this.include(query[j] + ".js");
				}//for...				
			}//if...
			else {
				return false;
			}
		}//if...
		else {
			return false;
		}			e
	},//loadByQueryString()...
	/**
	 * @private
	 * Does the work of checking if the pageName matches the file and sends it off to 
	 * be loaded.
	 * @param {Object} obj
	 * @param {String} pageName
	 */
	_loadUp: function(obj, pageName) {
		for (var script in obj) {
			if (typeof(obj[script]) == 'string') {
				if ((pageName == obj[script]) || (obj[script] == 'all')) {
					this.include(script + '.js');
				}//if...
			}//if...
			else {
				for (var i = 0, len = obj[script].length; i < len; ++i) {
					if (obj[script][i] == pageName) {
						this.include(script + '.js');
					}//if...			
				}//for...
			}//else...			
		}//for...in...		
	},
	/**
	 * @return {String} file and path name
	 */
	getFileName: function() {
		var path = location.pathname;
		var dotLocation = path.lastIndexOf('.');
		
		return path.substring(0, dotLocation);		
	},//getFileName()...
	/**
	 * @return {String} pageName
	 */
	getMetaName: function() {
		var metaElements = document.getElementsByTagName('meta');
		var i = 0;
		while (i < metaElements.length) {
			if (metaElements[i].hasAttribute('name')) {
				var pageName = metaElements[i].getAttribute('name').toLowerCase();
				if (pageName == 'pagename') {
					return metaElements[i].getAttribute('content');
				}				
			}
			i++;
		}//while...	
		return null;		
	},//getMetaName()...
	/**
	 * @return {String} <head> tag ID
	 */
	getHeadElement: document.getElementsByTagName('head')[0].id,//getHeadElement()...
	/**
	 * If you don't specify a specific method to check for a pageName.  We use this function to check
	 * all the different areas for a pageName.  
	 * The sequence is as follows:
	 * 1) head
	 * 2) meta
	 * 3) file name
	 * @return {String} pageName || null
	 */
	getPageLocation: function() {
		if (this.getHeadElement == '') {
			var metaValue = this.getMetaName();
			if (metaValue == null) {
				return this.fileName();
			}//if...
			else {
				return metaValue;
			}//else...
		}//if...
		else {
			return this.getHeadElement;
		}//else...

		return null
	},//getPageLocation()...
	
	/**
	 * finds the <script> tag that has the ID of initScript, this is actually the script that called this function, so it can always be found
	 * @return {Element} ID
	 */
	findInitiScript: document.getElementById('initScript'),//findInitScript()...
	/**
	 * grabs the initScript and gets its path. For this JS to work correctly, init.js must be in the root level of ALL the JavaScript files.  This is
	 * a convention that must be followed.
	 * @return {String} path || null
	 */
	findPath: document.getElementById('initScript').src.replace(/init\.js(\?.*)?$/, ''),//findPath()...
	
	/**
	 * We can't do a fancy DOM inclusion for the scripts that get added in, some browsers just won't run them.
	 * Yea, I am looking at you Safari 1.3
	 * @param {String} includer name of the file you want to load
	 */
	include: function(includer) {
		document.write('<script type="text/javascript" src="'+this.findPath+includer+'"></script>');		
	}//include()...
};//LoadScripts...


/**
 * Any work that must be done <em>before</em> the page loads.  
 * @class
 */
var Preload = {
	/**
	 * Image Preloading function.  Loads up the images then kills the array to save memory.  
	 * @param {String} dir directory where all the images are kept
	 * @param {String} imgs[] array of images to load up
	 */
	images:function(dir, imgs) {
		var loader = new Array();
		var len = imgs.length;
		for (var i = 0; i <= len; ++i) {
			loader[i] = new Image();
			loader[i].src = dir + imgs[i];
		}
		loader = null;
	},
	/**
	 * If you are using a particular browser, you can load up a CSS file for that particular browser only
	 * @param {String} browserName the browser that you want to check for
	 * @param {String} file the full path and file name that you want to load
	 */
	cssWhenBrowserIs: function(browserName, file){
		var checker;
		switch (browserName.toLowerCase()) {
			case 'ie':
				checker = !!(window.attachEvent && !window.opera);
				break;
			case 'safari':
				checker = !!(navigator.vendor);
				break;
			case 'safari3':
				checker = navigator.userAgent.indexOf('AppleWebKit/') > -1;
				break;	
			case 'firefox':
				checker = navigator.userAgent.indexOf('Gecko') > -1 && navigator.userAgent.indexOf('KHTML') == -1;
				break;
			case 'opera':
				checker = !!(window.opera);
				break;
			default:
				break;
		}//switch()...
		if (checker) {
			document.write('<link rel="stylesheet" type="text/css" href="'+file+'"></script>');
		}//if...
	}		
};
LoadScripts.loadByFileName({
	'library/prototype':'all',
	'loader':'all'
});